package www.dianbaozu.com.ui.chat.friendRequest

import android.util.Log
import androidx.hilt.lifecycle.ViewModelInject
import androidx.lifecycle.viewModelScope
import com.netease.nim.uikit.api.NIMHelper
import com.netease.nim.uikit.api.NIMRepository
import com.netease.nimlib.sdk.RequestCallback
import com.netease.nimlib.sdk.msg.model.SystemMessage
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import www.dianbaozu.com.R
import www.wen.com.common.lib.base.module.BaseViewModel
import www.wen.com.common.lib.data.entities.LoadingDialog
import www.wen.com.common.lib.data.entities.LoadingDialogStateType
import www.wen.com.common.lib.data.entities.Result
import www.wen.com.common.lib.data.entities.UiStateCustom
import www.wen.com.common.lib.extension.getString
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException

class FriendRequestViewModel @ViewModelInject constructor() :
    BaseViewModel() {

    @FlowPreview
    fun getAddFriendMsg(
        offset: Int,
        limit: Int,
        success: ((Result.Success<List<SystemMessage>>) -> Unit),
        error: ((String) -> Unit)
    ) {
        viewModelScope.launch {
            flow {
                try {
                    emit(Result.Success(NIMRepository.getAddFriendMsg(offset, limit)))
                } catch (e: Exception) {
//                    Result.Error(e)
                    throw e
                }
            }.retryWhen { cause, attempt ->
                return@retryWhen cause.message == "网络异常" && attempt <= 1
            }
                .onStart {
                    loading.value = UiStateCustom(
                        stateData = LoadingDialog(
                            "",
                            LoadingDialogStateType.LOADING
                        )
                    )
                }
                .onCompletion { cause ->
                    if (cause == null) {
                        loading.value = UiStateCustom(
                            stateData = LoadingDialog(
                                tip = "",
                                loadingDialogStateType = LoadingDialogStateType.SUCCESS,
                                isShowDialogTip = false,
                                isShowToastTip = false
                            )
                        )
                    }
                }
                .catch { cause ->
                    loading.value = UiStateCustom(
                        stateData = LoadingDialog(
                            tip = cause.message ?: "网络异常",
                            loadingDialogStateType = LoadingDialogStateType.ERROR,
                            isShowDialogTip = false,
                            isShowToastTip = true
                        )
                    )
                    error.invoke(cause.message ?: "网络异常")
                }
                .collect {
                    success.invoke(it)
                }
        }
    }

}